#include "mainscence.h"
#include "config.h"
#include<QIcon>
#include<QPainter>
#include<QMouseEvent>
#include<ctime>
MainScence::MainScence(QWidget *parent)
    : QMainWindow(parent)
{
    initScence();

    playGame();
}

MainScence::~MainScence()
{
}
void MainScence::initScence()
{
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);

    setWindowTitle(GAME_TITLE);

    setWindowIcon(QIcon(GAME_RES_PATH));

    m_Timer.setInterval(GAME_RATE);

    m_recorder = 0;

    srand(  (unsigned int)time(NULL));
}

void MainScence::playGame()
{
    m_Timer.start();

    connect(&m_Timer , &QTimer::timeout,[=](){

        enemyToScence();

        updatePosition();

        update();

        collisionDetection();
    });
}

void MainScence::updatePosition()
{
    m_map.mapPosition();

    m_hero.shoot();

    for(int i=0;i<BULLET_NUM;i++)
    {
        if(m_hero.m_bullets[i].m_Free==false)
        {
            m_hero.m_bullets[i].updatePosition();
        }
    }
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free==false)
        {
            m_enemys[i].updatePosition();
        }
    }


    //temp_Bullet.m_Free = false;
    //temp_Bullet.updatePosition();
}

void MainScence::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    painter.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);

    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Ninja);


    for (int i=0;i< BULLET_NUM;i++)
    {
        if(m_hero.m_bullets[i].m_Free==false)
        {
            painter.drawPixmap(m_hero.m_bullets[i].m_X,m_hero.m_bullets[i].m_Y,m_hero.m_bullets[i].m_Bullet);
        }
    }

    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free==false)
        {
            painter.drawPixmap(m_enemys[i].m_X,m_enemys[i].m_Y,m_enemys[i].m_enemy);
        }
    }


    //painter.drawPixmap(temp_Bullet.m_X,temp_Bullet.m_Y,temp_Bullet.m_Bullet);
}

void MainScence::mouseMoveEvent(QMouseEvent * event)
{

    int x =event -> x() - m_hero.m_Rect.width()*0.5;
    int y =event -> y() - m_hero.m_Rect.height()*0.5;

    if(x<=0)
    {
        x=0;
    }
    if(x>=GAME_WIDTH-m_hero.m_Rect.width())
    {
        x=GAME_WIDTH-m_hero.m_Rect.width();
    }
    if(y<=0)
    {
        y=0;
    }
    if(y>=GAME_HEIGHT-m_hero.m_Rect.height())
    {
        x=GAME_HEIGHT-m_hero.m_Rect.height();
    }
    m_hero.setPosition(x,y);
}

void MainScence::collisionDetection()
{
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free)
        {
            continue;
        }
        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_hero.m_bullets[j].m_Free)
            {
                continue;
            }

            if(m_enemys[i].m_Rect.intersects(m_hero.m_bullets[j].m_Rect))
            {
                m_enemys[i].m_Free=true;
                m_hero.m_bullets[j].m_Free=true;



            }
        }
    }
}

void MainScence::enemyToScence()
{
    m_recorder++;
    if(m_recorder < ENEMY_INTERVAL)
    {
        return;
    }
    m_recorder =0;

    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free)
        {
            m_enemys[i].m_Free = false;

            m_enemys[i].m_X = rand()%(GAME_WIDTH-m_enemys[i].m_Rect.width());
            m_enemys[i].m_Y = -m_enemys[i].m_Rect.height();
            break;
        }
    }
}
